package com.mcxx.util;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;


public class FileOperater {


    public static String downloadFile(String fileCatch, String urlString){
        String filename = null;
        FileOutputStream os = null;
        InputStream is = null;
        try {
            URL url = new URL(urlString);
            URLConnection con = url.openConnection();
           // is = ImgUtil.compress(con.getInputStream());
            is = con.getInputStream();
            byte[] bs = new byte[2048];
            int len;

            File directory = new File(fileCatch);
            if (!directory .exists()  && !directory .isDirectory()) {
                directory.mkdirs();
            }

            filename =  fileCatch + getFileNameByUrl(urlString)+".jpg";
            File file = new File(filename);
            os = new FileOutputStream(file, true);
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return filename;
    }

    public static File createFile(String filePath) {
        File file = new File(filePath);
        try{
            if(!file.exists()) {
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
        return file;
    }

    public static File mkDirs(String path) {
        File dir = new File(path);
        if (!dir.exists()) {

            dir.mkdirs();
        }
        return dir;
    }

    public static String getFileNameByUrl(String url) {
        return url.substring(url.lastIndexOf("/")+1, url.indexOf("?"));
    }

    public static boolean isFileExists(String filePath) {
        File file = new File(filePath);
        return file.exists();
    }


    public static boolean deleteFile(String path) {
        boolean flag = false;
        System.out.println("deletefile="+path);
        if(StringUtils.isNotEmpty(path) && !path.equals("#")) {
            System.out.println("deletefile1111="+path);
            File file = new File(path);

            if (file.isFile() && file.exists()) {
                file.delete();
                flag = true;
            }
        }
        return flag;
    }


    public static boolean deleteFiles(String[] fileCatch) {
        for(int i = 0; i < fileCatch.length; i++) {
            deleteFile(fileCatch[i]);
        }
        return true;
    }

    public static boolean deleteFilesByUrl(String fileCatch, String[] url) {
        for(int i = 0; i < url.length; i++) {
            deleteFile(fileCatch + getFileNameByUrl(url[i]));
        }
        return true;
    }

    public static File transBytesToFile(byte[] bytes, String fileName) {
        File file = createFile(fileName);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(bytes, 0, bytes.length);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return file;
    }

    public static String getFileSuffix(String name) {
        return name.substring(name.lastIndexOf(".")+1);
    }
}
